<?php

if(trim($argv[1]) == '' || trim($argv[2]) == '')
{
die('
=======================================
	Smit Source File Changer
=======================================
	Usage: version.php <from> <to>
---------------------------------------
');
}

$version = trim($argv[1]);
$version2 = trim($argv[2]);

$path = 'D:/www/smit/';

$excluded = array('dev', 'components', 'geshi', 'decoda', 'styles', 'assets');
$i = 0;
$files = array();

function replace($dir = '')
{
	global $excluded, $version, $version2, $i, $files;

	$iterator = new DirectoryIterator($dir);
	foreach ($iterator as $fileinfo) {
		$ext = strtolower(strrchr($fileinfo->getFilename(), '.'));
		if (!$fileinfo->isDot() && ($ext == '.php' || $ext == '') && !in_array(strtolower($fileinfo->getFilename()), $excluded) && stristr($fileinfo->getFilename(), 'sbk.')) {
			//echo $iterator->getPath() . '/' . $fileinfo->getFilename() . chr(10);
			switch($iterator->getType())
			{
				case 'file':
					$contents = file_get_contents($iterator->getPath() . '/' . $fileinfo->getFilename());
					if(stristr($contents, '*	This is Smit Matterhorn - Build '.$version))
					{
						$i++;
						$files[] = $iterator->getPath() . '/' . $fileinfo->getFilename();
						file_put_contents($iterator->getPath() . '/' . 'sbk.' . $fileinfo->getFilename(), $contents);
						$contents = str_replace('This is Smit Matterhorn - Build '.$version, 'This is Smit Matterhorn - Build '.$version2, $contents);
						file_put_contents($iterator->getPath() . '/' . $fileinfo->getFilename(), $contents);
					}
				break;
				case 'dir':
					replace($iterator->getPath() . '/' . $fileinfo->getFilename() . '/');
				break;
			}
		}
	}
}

replace($path);

echo
'
-------------------------------------------------------
	Updated '.$i.' Files to Version '.$version2.'
	
	The Files are listed for Clarification
	'.implode("\n\t", $files).'
-------------------------------------------------------
';
?>